import request from '@/utils/request'
import { downloadFileByType } from '@/utils/util'

const api = {
  pmmlModelList: '/modelApi/modelManage/pmmlModelList',
  versionDetail: '/modelApi/modelManage/versionDetail',
  pmmlModelStatus: '/modelApi/modelManage/enabled',
  pmmlManageModels: '/modelApi/modelManage/manageModels', // 删除模型
  pmmlDownload: '/modelApi/modelManage/pmmlDownload', // 下载模型
  templateDownload: '/modelApi/modelManage/template', // 模板下载
  pmmlModelUpload: '/modelApi/modelManage/pmmlModel', // 上传模型
  // 模型测试
  pmmlModelCalculate: '/modelApi/pmmlModelCalculate',
}
const boundary = '----WebKitFormBoundary' + Math.random().toString(36).slice(2)
export function getPMMLModelList(parameter) {
  return request({
    url: api.pmmlModelList,
    method: 'get',
    params: parameter,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
  })
}

export function getVersionDetail(parameter) {
  return request({
    url: api.versionDetail,
    method: 'get',
    params: parameter,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
  })
}
/**
 * 设置PMML模型状态(模型上下线)
 */
export function setPMMLModelStatus(parameter) {
  return request({
    url: api.pmmlModelStatus,
    method: 'put',
    data: parameter,
    formData: true,
  })
}
/**
 * 删除PMML模型
 */
export function deletePMMLModel(parameter) {
  return request({
    url: api.pmmlManageModels,
    method: 'delete',
    data: parameter,
    formData: true,
  })
}
/**
 * 下载PMML模型
 */
export function downloadPMMLModel(parameter, option) {
  return downloadFileByType(request, api.pmmlDownload, parameter, option)
}
/**
 * 模板下载
 */
export function templateDownload(parameter, option) {
  return downloadFileByType(request, api.templateDownload, parameter, option)
}
/**
 * 上传PMML模型
 */
export function uploadPMMLModel(parameter) {
  return request({
    url: api.pmmlModelUpload,
    method: 'post',
    data: parameter,
    'Content-Type': `multipart/form-data; boundary=${boundary}`,
  })
}
/**
 * 模型测试
 */
export function pmmlModelCalculate(parameter) {
  return request({
    url: api.pmmlModelCalculate,
    method: 'get',
    params: parameter,
    headers: {
      'Content-Type': 'application/json;charset=UTF-8',
    },
  })
}
